package com.axx.android.profile.message.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.axx.android.R;
import com.axx.android.base.BaseActivity;
import com.axx.android.common.Constants;
import com.axx.android.home.model.User;
import com.axx.android.letter.activity.LetterDetailActivity;
import com.axx.android.letter.activity.RecipientActivity;
import com.axx.android.profile.message.adapter.LetterAdapter;
import com.axx.android.profile.message.model.MessageLetter;
import com.axx.android.utils.GsonUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONException;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MessageContractActivity extends BaseActivity {


    @Bind(R.id.btn_back)
    Button btnBack;
    @Bind(R.id.lv_letter)
    RecyclerView lvLetter;
    @Bind(R.id.activity_message)
    LinearLayout activityMessage;

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_contract;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        final List<MessageLetter.LetterListBean.BodyLetterBean> list = (List<MessageLetter.LetterListBean.BodyLetterBean>) getIntent().getSerializableExtra("data");

        final LetterAdapter letterAdapter = new LetterAdapter(R.layout.view_item_message_letter, list);
        lvLetter.setAdapter(letterAdapter);
        lvLetter.setLayoutManager(new LinearLayoutManager(this));
        lvLetter.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
        letterAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(MessageContractActivity.this,LetterDetailActivity.class);
                intent.putExtra("id", list.get(position).getId());
                intent.putExtra("from", "detail");
                startActivity(intent);
            }
        });

        letterAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                String jsonString = null;
                switch (view.getId()) {
                    case R.id.iv_to_header:
                        jsonString = GsonUtils.toJson(letterAdapter.getData().get(position).getToUser());
                        break;
                    case R.id.iv_from_header:
                        jsonString = GsonUtils.toJson(letterAdapter.getData().get(position).getFromUser());
                        break;
                }
                startUserActivity(jsonString);
            }

        });
    }

    private void startUserActivity(String jsonString) {
        try {
            User.UserListBean userListBean1 = GsonUtils.fromJson(jsonString, new TypeToken<User.UserListBean>() {
            });
            Intent intent = new Intent(MessageContractActivity.this, RecipientActivity.class);
            intent.putExtra("model", userListBean1);
            startActivity(intent);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
